package com.jdz.gxq.info.admin.smallloan.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.smallloan.bo.Smallloan;
import com.jdz.gxq.info.admin.smallloan.form.AddSmallloanForm;
import com.jdz.gxq.info.admin.smallloan.service.SmallloanService;
import com.jdz.gxq.info.common.action.CommonAction;

public class AddSmallloanAction extends CommonAction {

	private SmallloanService smallloanService = null;

	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {

		AddSmallloanForm addSmallloanFrom = (AddSmallloanForm) form;

		Smallloan smallloan = addSmallloanFrom.getSmallloan();

		// smallloan名重复验证(其他后台逻辑验证也类似处理)
		if (!smallloanService.checkSmallloanName(smallloan, 1)) {
			ActionMessages messages = new ActionMessages();
			messages.add("smallloan.recipientName", new ActionMessage(
					"error.smallloanNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		smallloan.setModifiedBy(getUserId(request));

		smallloanService.insertSmallloan(smallloan);

		return mapping.findForward("success");
	}

	public void setSmallloanService(SmallloanService smallloanService) {
		this.smallloanService = smallloanService;
	}
}
